package org.wangchaocheng.cloudgallery.user.gatewayimpl;

import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.wangchaocheng.cloudgallery.common.utils.IBatisPlusWrapperBuilder;
import org.wangchaocheng.cloudgallery.dto.common.PageDTO;
import org.wangchaocheng.cloudgallery.dto.user.UserDTO;
import org.wangchaocheng.cloudgallery.query.user.UserPageQry;
import org.wangchaocheng.cloudgallery.user.converter.UserConverter;
import org.wangchaocheng.cloudgallery.user.dataObject.UserDO;
import org.wangchaocheng.cloudgallery.user.gateway.UserGateway;
import org.wangchaocheng.cloudgallery.user.mapper.UserMapper;

/**
 * @author wangchaocheng
 * @date 2025-03-01 10:30:19
 */
@Repository
public class UserGatewayImpl extends ServiceImpl<UserMapper, UserDO> implements UserGateway {

    @Override
    public void create(UserDTO userDTO) {
        UserDO userDO = UserConverter.INSTANCE.dto2Do(userDTO);
        this.baseMapper.insert(userDO);
    }

    @Override
    public void modify(UserDTO userDTO) {
        UserDO userDO = UserConverter.INSTANCE.dto2Do(userDTO);
        this.baseMapper.updateById(userDO);
    }

    @Override
    public <T> UserDTO findByColumn(SFunction<UserDTO, ?> column, T value) {
        QueryWrapper<UserDO> queryWrapper = IBatisPlusWrapperBuilder.buildSignCloumnQueryWrapper(column, new UserDO(), value);
        if (Objects.isNull(queryWrapper)){
            return null;
        }
        UserDO userDO = baseMapper.selectOne(queryWrapper);
        return UserConverter.INSTANCE.do2Dto(userDO);
    }

    @Override
    public <T> Boolean existByColumn(SFunction<UserDTO, ?> column, T value) {
        QueryWrapper<UserDO> queryWrapper = IBatisPlusWrapperBuilder.buildSignCloumnQueryWrapper(column, new UserDO(), value);
        if (Objects.nonNull(queryWrapper)){
            return exists(queryWrapper);
        }
        return Boolean.FALSE;
    }

    @Override
    public PageDTO<UserDTO> listPage(UserPageQry qry) {
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<UserDO>()
            .like(StringUtils.isNotBlank(qry.getUsername()), UserDO::getUsername, qry.getUsername())
            .like(StringUtils.isNotBlank(qry.getNickname()), UserDO::getNickname, qry.getNickname())
            .like(StringUtils.isNotBlank(qry.getPhoneNumber()), UserDO::getPhoneNumber, qry.getPhoneNumber())
            .like(StringUtils.isNotBlank(qry.getEmail()), UserDO::getEmail, qry.getEmail())
            .eq(StringUtils.isNotBlank(qry.getStatus()), UserDO::getStatus, qry.getStatus());
        // 排序
        if (StringUtils.isNotBlank(qry.getOrderBy())){
            IBatisPlusWrapperBuilder.appendDynamicOrderColumn(queryWrapper, UserDO.class, qry.getOrderBy(), qry.isOrderByAsc());
        }
        // 查询分页
        Page<UserDO> userDOPage = baseMapper.selectPage(new Page<>(qry.getPageIndex(), qry.getPageSize()), queryWrapper);
        return UserConverter.INSTANCE.toPageDTO(userDOPage);
    }

}




